package tpdisenio2009.mvp.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import tpdisenio2009.Constants;
import tpdisenio2009.Tpdisenio2009App;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.mvp.display.TournamentDateWinnersView;
import tpdisenio2009.mvp.display.WinnersPrinterPreviewViewImpl;
import tpdisenio2009.services.FantasyTeamPlayerService;
import tpdisenio2009.services.FantasyTeamScoreByTournamentDateService;
import tpdisenio2009.services.TournamentDateService;
import tpdisenio2009.services.UserService;

/**
 *
 * @author Pablito
 */
public class TournamentDateWinnersPresenter extends DefaultPresenter<TournamentDateWinnersView> {

    private User user;
    private final UserService userService;
    private final TournamentDateService tournamentDateService;    
    private final FantasyTeamPlayerService fantasyTeamPlayerService;
    private final FantasyTeamScoreByTournamentDateService fantasyTeamScoreService;

    private ItemListener dateChangeListener;
    private ChangeListener pageChangeListener;
    private ActionListener lookPrinterListener;

    public TournamentDateWinnersPresenter( TournamentDateWinnersView view ) {
        super( view );

        userService = new UserService();
        tournamentDateService = new TournamentDateService();        
        fantasyTeamPlayerService = new FantasyTeamPlayerService();
        fantasyTeamScoreService = new FantasyTeamScoreByTournamentDateService();
        
        
        if ( Tpdisenio2009App.loggedUser instanceof User) {
            user = (User) Tpdisenio2009App.loggedUser;
            getView().setMaxPage( userService.countUsers() / Constants.PAGE_SIZE + 1 );
            getView().setDates( tournamentDateService.getClosedTournamentDates() );
            getView().setFantasyTeamName( user.getFantasyTeam().getName() );
            getView().setOwnPoints( fantasyTeamScoreService.getFantasyTeamPointsByDate( user.getFantasyTeam() , getView().getDate() ) );
            getView().setOwnPosition( fantasyTeamScoreService.getFantasyTeamPositionByDate( user.getFantasyTeam() , getView().getDate() ) );
            getView().setPage( 1 );
            loadFantasyTeamsTable( 0 );
            bind();
        } else {
            getView().showNotification( "<html> Su usuario es incorrecto. <br>Compruebe que no sea un Administrador o un Periodista.</html>", "Usuario Incorrecto" );
        }
    }

    public void bind() {
        dateChangeListener = new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                getView().setOwnPoints( fantasyTeamScoreService.getFantasyTeamPointsByDate( user.getFantasyTeam() , getView().getDate() ) );
                getView().setOwnPosition( fantasyTeamScoreService.getFantasyTeamPositionByDate( user.getFantasyTeam() , getView().getDate() ) );
                getView().setPage( 1 );
                loadFantasyTeamsTable( 0 );
            }
        };
        getView().addDateChangeListener( dateChangeListener );

        pageChangeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int page = getView().getPage();
                loadFantasyTeamsTable( page - 1 );
            }
        };
        getView().addPageChangeListener( pageChangeListener );

        lookPrinterListener = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {                
                WinnersPrinterPreviewPresenter presenter = new WinnersPrinterPreviewPresenter( new WinnersPrinterPreviewViewImpl( null, true, getView().getDate() ) );
                presenter.getView().asWidget().setVisible( true );
            }
        };
        getView().addLookPrinterListListener( lookPrinterListener );        
    }

    public void unbind() {
        if (dateChangeListener != null)
            getView().removeDateChangeListener( dateChangeListener );
        if (pageChangeListener != null)
            getView().removePageChangeListener( pageChangeListener );
        if (lookPrinterListener != null)
            getView().removeLookPrinterListListener( lookPrinterListener );
    }

    public boolean hasDates() {
        return getView().getDate() != null;
    }

    private void loadFantasyTeamsTable( int page ) {
        TournamentDate date = getView().getDate();        
        List<FantasyTeamScoreByTournamentDate> fantasyTeamScores = fantasyTeamScoreService.getFantasyTeamScoresByTournamentDate( date, page );
        List<FantasyTeamPlayer> bestPlayers = fantasyTeamPlayerService.getBestPlayers(fantasyTeamScores, date);
        getView().loadFantasyTeams( fantasyTeamScores, bestPlayers );
    }
}
